package com.lz.ionicbase.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lz.ionicbase.R;
import com.lz.ionicbase.SuperApp;
import com.lz.ionicbase.engine.callback.AppCallback;
import com.lz.ionicbase.model.NewPsdBean;
import com.lz.ionicbase.utils.BaseUtil;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.EditTextUtils;
import com.lz.ionicbase.utils.SpUtils;
import com.lz.ionicbase.utils.ToastUtils;
import com.lzy.okgo.OkGo;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by lenovo on 2017/2/20.
 */

public class PsdSetLoginActivity extends BaseActivity {
    @InjectView(R.id.my_back)
    ImageView myBack;
    @InjectView(R.id.psd_next)
    TextView psdNext;
    @InjectView(R.id.newpsd)
    EditText newpsd;
    private String vcode;
    private SpUtils sp;
    private String usernewpsd;
    private Context context = this;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_psd_login;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        vcode = getIntent().getStringExtra("vcode");
        sp = new SpUtils();
    }

    @Override
    protected void initListener() {
        EditTextUtils.setEditTextInhibitInputSpace(newpsd);
        EditTextUtils.setEditTextInhibitInputSpeChat(this, newpsd);
    }

    @OnClick({R.id.my_back, R.id.psd_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_back:
                finish();
                break;
            case R.id.psd_next:
                int length = this.newpsd.getText().toString().length();
                if (length < 6) {
                    Toast.makeText(this, "填写密码应大于6位数字", Toast.LENGTH_SHORT).show();
                } else {
                    String netUrl = sp.getString(PsdSetLoginActivity.this, Constant.NET_URL, "");
                    String username = sp.getString(PsdSetLoginActivity.this, Constant.ACCOUNT, "");
                    String deviceId = sp.getString(PsdSetLoginActivity.this, Constant.DEVICEID, "");
                    String spLan = SpUtils.getString(getApplicationContext(), SpUtils.KEY_LANGUAGE, "简体中文");
                    String language = (String) SuperApp.lanMap.get(spLan)[0];
                    String base64username = BaseUtil.getBase64(username);
                    usernewpsd = newpsd.getText().toString();
                    String base64usernewpsd = BaseUtil.getBase64(usernewpsd);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("loginName", base64username);
                    map.put("password", base64usernewpsd);
                    map.put("uid", deviceId);
                    map.put("vcode", vcode);
                    map.put("language", language);
                    String requestUrl = netUrl + Constant.NEWPSDURL;

                    OkGo.post(requestUrl).tag(this).params(map)
                            .execute(new AppCallback<NewPsdBean>(context) {
                                @Override
                                public void onSuccess(NewPsdBean resObj, Call call, Response response) {
                                    if (resObj.success) {
                                        Intent intent = new Intent(PsdSetLoginActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                        sp.saveString(PsdSetLoginActivity.this, Constant.MIMA, usernewpsd);
                                        ToastUtils.showToast(PsdSetLoginActivity.this, resObj.getDesc(), Toast.LENGTH_SHORT);
                                    } else {
                                        ToastUtils.showToast(PsdSetLoginActivity.this, resObj.getDesc(), Toast.LENGTH_SHORT);
                                    }
                                }

                                @Override
                                public NewPsdBean convertSuccess(Response response) throws Exception {
                                    NewPsdBean resObj = new Gson().fromJson(response.body().string(), NewPsdBean.class);
                                    return resObj;
                                }
                            });
                }
                break;
        }
    }

}
